from   behave                        import *
from   myapp                         import app
from   selenium.webdriver.common.by  import By
from   selenium.webdriver.support.ui import WebDriverWait
from   selenium.webdriver.support    import expected_conditions as EC
from   selenium.common.exceptions    import TimeoutException
import traceback

exec(open(app.root_path + '/constants/tests_constants.py').read())


@given(u'I open a web browser')
def verify_selenium(context):
  assert str(type(context.browser)) == FIREFOX_WEBDRIVER


@when(u'I browse to "{url}"')
@when(u'I am at "{url}"')
def visit_url(context , url):
  context.browser.get(url)


@then(u'I should be on the "{url}" webpage')
def should_be_at_url(context , url):
  assert context.browser.current_url == url


@then(u'I should be on the login page')
def should_be_on_login_page(context):
  try: WebDriverWait(context.browser , 35).until(EC.title_contains(NOTABUG_LOGIN_TITLE))
  except(TimeoutException): traceback.print_exc()
  context.execute_steps('then I should be on the "' + NOTABUG_LOGIN_URL + '" webpage')


@then(u'I should see "{text}"')
def should_see(context , text):
  body = context.browser.find_element_by_tag_name('body')
  assert text in body.text


@then(u'I dump body')
def dump_body(context):
  body = context.browser.find_element_by_tag_name('body')
  print("body=\n" + body.text)
  assert False
